const router = require('koa-router')()
const fs = require('fs')


/*页面级路由*/
//router.get('/', async (ctx, next) => {
//	await ctx.render('./index.html')
//})

router.get('/agent/:channel', async (ctx, next) => {
  let query = ctx.query;
  let params = ctx.params;
  
  if (params.channel == 'WXoauth') {
    // 响应授权 https://qingjingzhi.com/agent/WXoauth?uri=https%3a%2f%2fqingjingzhi.com%2factivities&state=66798
    let state = query.state?query.state:'a0';
    let uri = 'https://open.weixin.qq.com/connect/oauth2/authorize?appid=wx3d51251099a35021&redirect_uri='+query.uri+'&response_type=code&scope=snsapi_userinfo&state='+state+'#wechat_redirect';
  	ctx.redirect(uri);
  } else if (params.channel == 'WXsilence'){
    // 静默授权
  	let state = query.state?query.state:'a0';
    let uri = 'https://open.weixin.qq.com/connect/oauth2/authorize?appid=wx3d51251099a35021&redirect_uri='+query.uri+'&response_type=code&scope=snsapi_base&state='+state+'#wechat_redirect';
    ctx.redirect(uri);
  }else{
    ctx.body = {errorMessage:'参数信息错误！'}
  }
})

router.get('/ctx', async (ctx, next) => {
  let params = {
    ctxRequest: ctx.request,
    ctxRip: ctx.request.ip,
    ctx: ctx
  }
  ctx.body = params;
})

module.exports = router
